<template>
  <div class="admin-layout">
    <el-container>
      <el-aside width="200px">
        <div class="logo">
          <img src="@/assets/images/logo.jpg" alt="logo" />
          <span>管理后台</span>
        </div>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical"
          :router="true"
        >
          <el-menu-item index="/admin/goods">
            <el-icon><Goods /></el-icon>
            <span>商品管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/chat">
            <el-icon><ChatLineRound /></el-icon>
            <span>聊天管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/hot-services">
            <el-icon><Service /></el-icon>
            <span>热门服务管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/order">
            <el-icon><List /></el-icon>
            <span>订单管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/address">
            <el-icon><Location /></el-icon>
            <span>地址管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/shopCar">
            <el-icon><ShoppingCart /></el-icon>
            <span>购物车管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/user-center">
            <el-icon><User /></el-icon>
            <span>个人中心</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <div class="header-content">
            <div class="left">
              <my-breadcrumb />
            </div>
            <div class="right">
              <el-dropdown>
                <span class="user-info">
                  <el-avatar :size="32" :src="IMAGE_URL + userStore.user?.avatar" />
                  <span class="username">{{ userStore.user?.nickName }}</span>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="$router.push('/admin/user-center')">
                      <el-icon><User /></el-icon>
                      <span>个人中心</span>
                    </el-dropdown-item>
                    <el-dropdown-item @click="handleLogout">
                      <el-icon><SwitchButton /></el-icon>
                      <span>退出登录</span>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/modules/user'
import { ElMessageBox } from 'element-plus'
import {
  Goods,
  ChatLineRound,
  Service,
  List,
  Location,
  ShoppingCart,
  User,
  SwitchButton
} from '@element-plus/icons-vue'
import MyBreadcrumb from '@/components/MyBreadcrumb.vue'
import { IMAGE_URL } from '@/utils/request'
const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

// 当前激活的菜单
const activeMenu = computed(() => route.path)

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logout()
    router.push('/login')
  })
}
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  background-color: #f5f7fa;
}

.el-container {
  height: 100%;
}

.el-aside {
  background-color: #304156;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 1000;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  background-color: #263445;
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}

.logo img {
  width: 32px;
  height: 32px;
  margin-right: 12px;
}

.el-menu {
  border-right: none;
  background-color: transparent;
  height: calc(100% - 60px);
  overflow-y: auto;
}

.el-menu::-webkit-scrollbar {
  width: 0;
}

.el-menu-item {
  color: #bfcbd9;
  height: 50px;
  line-height: 50px;
}

.el-menu-item:hover {
  background-color: #263445 !important;
}

.el-menu-item.is-active {
  background-color: #263445 !important;
  color: #409EFF;
}

.el-menu-item .el-icon {
  margin-right: 8px;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  position: fixed;
  right: 0;
  left: 200px;
  z-index: 999;
}

.header-content {
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 0 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.username {
  margin-left: 8px;
  font-size: 14px;
  color: #606266;
}

.el-main {
  background-color: #f5f7fa;
  padding: 20px;
  margin-left: 200px;
  margin-top: 60px;
  min-height: calc(100vh - 60px);
  overflow-y: auto;
}

:deep(.el-card) {
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  margin-bottom: 20px;
}

:deep(.el-card__header) {
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
  background-color: #fff;
}

:deep(.el-card__body) {
  padding: 20px;
}
</style> 